package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.SysPtQuestionAddBo;
import com.ruoyi.system.bo.SysPtQuestionQueryBo;
import com.ruoyi.system.bo.SysPtQuestionEditBo;
import com.ruoyi.system.domain.SysPtQuestion;
import com.ruoyi.system.mapper.SysPtQuestionMapper;
import com.ruoyi.system.vo.SysPtQuestionVo;
import com.ruoyi.system.service.ISysPtQuestionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验题目Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-28
 */
@Service
public class SysPtQuestionServiceImpl extends ServiceImpl<SysPtQuestionMapper, SysPtQuestion> implements ISysPtQuestionService {

    @Override
    public SysPtQuestionVo queryById(Long questionId){
        return getVoById(questionId, SysPtQuestionVo.class);
    }

    @Override
    public TableDataInfo<SysPtQuestionVo> queryPageList(SysPtQuestionQueryBo bo) {
        PagePlus<SysPtQuestion, SysPtQuestionVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPtQuestionVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPtQuestionVo> queryList(SysPtQuestionQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPtQuestionVo.class);
    }

    private LambdaQueryWrapper<SysPtQuestion> buildQueryWrapper(SysPtQuestionQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPtQuestion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExpId() != null, SysPtQuestion::getExpId, bo.getExpId());
        lqw.eq(StrUtil.isNotBlank(bo.getQuestionNo()), SysPtQuestion::getQuestionNo, bo.getQuestionNo());
        lqw.eq(StrUtil.isNotBlank(bo.getQuestion()), SysPtQuestion::getQuestion, bo.getQuestion());
        lqw.eq(StrUtil.isNotBlank(bo.getOption()), SysPtQuestion::getOption, bo.getOption());
        lqw.eq(StrUtil.isNotBlank(bo.getAnswer()), SysPtQuestion::getAnswer, bo.getAnswer());
        lqw.eq(StrUtil.isNotBlank(bo.getAnalysis()), SysPtQuestion::getAnalysis, bo.getAnalysis());
        lqw.eq(bo.getScore() != null, SysPtQuestion::getScore, bo.getScore());
        lqw.eq(bo.getType() != null, SysPtQuestion::getType, bo.getType());
        lqw.eq(bo.getIsEnable() != null, SysPtQuestion::getIsEnable, bo.getIsEnable());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPtQuestionAddBo bo) {
        SysPtQuestion add = BeanUtil.toBean(bo, SysPtQuestion.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPtQuestionEditBo bo) {
        SysPtQuestion update = BeanUtil.toBean(bo, SysPtQuestion.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPtQuestion entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
